package com.wjy.icu.Delay;

import cn.hutool.core.util.HexUtil;
import com.wjy.icu.emqx.client.MqttPushClient;
import com.wjy.icu.emqx.utils.MqttUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.Executors;

/**
 * @author
 * @Time 2020/11/23 10:59
 * @description: DelayQueueManager延时任务管理类
 */

@Component
public class DelayQueueManager implements CommandLineRunner {

    @Autowired
    private MqttPushClient mqttPushClient;

    private final Logger logger = LoggerFactory.getLogger(DelayQueueManager.class);
    private DelayQueue<DelayTask> delayQueue = new DelayQueue<>();

    /**
     * 加入到延时队列中
     * @param task
     */
    public void put(DelayTask task) {
        logger.info("加入延时任务：{}", task);
        delayQueue.put(task);
    }

    /**
     * 取消延时任务
     * @param task
     * @return
     */
    public boolean remove(DelayTask task) {
        logger.info("取消延时任务：{}", task);
        return delayQueue.remove(task);
    }

    /**
     * 取消延时任务
     * @param taskid
     * @return
     */
    public boolean remove(String taskid) {
        return remove(new DelayTask(new EquipmentMq(null,taskid), 0));
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info("初始化延时队列");
        Executors.newSingleThreadExecutor().execute(new Thread(this::excuteThread));
    }

    /**
     * 延时任务执行线程
     */
    private void excuteThread() {
        while (true) {
            try {
                DelayTask task = delayQueue.take();
                processTask(task);
            } catch (InterruptedException e) {
                break;
            }
        }
    }

    /**
     * 内部执行延时任务
     * @param task
     */
    private void processTask(DelayTask task) {
        logger.info("执行延时任务：{}", task);
        EquipmentMq data = task.getData();
        String mac = data.getIdentifier();
        switch (data.getType()){
            case 0:
                logger.info("发送订阅指令");
                mqttPushClient.subscribe(mac+"/up",0);
                logger.info("发送订阅指令完成");
                break;
            case 1:
                logger.info("发送设备上传指令");
                //根据task中的data自定义数据来处理相关逻辑，例 if (task.getData() instanceof XXX) {}
                byte[] bytes = HexUtil.decodeHex("680202010116");
                String s1 = MqttUtils.Bytes2HexString(bytes);
                System.out.println("message:"+s1 );
                mqttPushClient.publish(0,false,mac+"/down",bytes);
                System.out.println("message成功");
                break;
            default:

        }

    }

}

